package com.cv.platform.service.report;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.infra.api.websocket.WebSocketSenderApi;
import com.cv.platform.controller.audit.report.vo.AuditReportPageReqVO;
import com.cv.platform.controller.audit.report.vo.AuditReportRespVO;
import com.cv.platform.controller.platform.message.vo.conversation.CvConversationSaveReqVO;
import com.cv.platform.controller.platform.message.vo.record.CvMessageSaveReqVO;
import com.cv.platform.controller.platform.report.vo.CvReportSaveReqVO;
import com.cv.platform.dal.dataobject.report.CvReportDO;
import com.cv.platform.dal.mapper.report.CvReportMapper;
import com.cv.platform.enums.ReportStatusEnum;
import com.cv.platform.enums.ReportTypeEnum;
import com.cv.platform.service.comment.CvCommentService;
import com.cv.platform.service.message.CvConversationService;
import com.cv.platform.service.message.CvMessageService;
import com.cv.platform.service.video.CvVideoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 举报业务层接口实现类
 *
 * @author Charles_XDXD
 */
@Service
public class CvReportServiceImpl implements CvReportService {

    @Resource
    private CvReportMapper cvReportMapper;

    @Resource
    private CvConversationService cvConversationService;

    @Resource
    private CvMessageService cvMessageService;

    @Resource
    @Lazy
    private CvCommentService cvCommentService;

    @Resource
    private CvVideoService cvVideoService;

    @DubboReference
    private WebSocketSenderApi socketSenderApi;

    @Override
    public Long saveReport(CvReportSaveReqVO reqVO) {
        CvReportDO cvReportDO = BeanUtils.toBean(reqVO, CvReportDO.class);
        Long recordId = cvReportDO.getId();
        if (ObjUtil.isEmpty(cvReportDO.getReportUserId())) {
            cvReportDO.setReportUserId(SecurityFrameworkUtil.getLoginUserId());
        }

        if (ObjUtil.isNotEmpty(recordId)) {
            Long userId = cvReportDO.getReportUserId();
            Long conversationId = cvConversationService.getConversationId(SecurityFrameworkUtil.getLoginUserId(), userId);
            if (ObjUtil.isEmpty(conversationId)) {
                conversationId = cvConversationService.saveConversation(new CvConversationSaveReqVO()
                        .setTargetUserId(userId)
                );
            }

            cvMessageService.saveMessage(new CvMessageSaveReqVO()
                    .setContent("您举报的" + (cvReportDO.getReportType().equals(ReportTypeEnum.VIDEO.getCode()) ?
                            "视频《" + cvVideoService.getVideoDetail(cvReportDO.getChildrenId(), true).getTitle() + "》 情况不属实，现已驳回"
                            : "评论：'" + cvCommentService.getCommentContent(cvReportDO.getChildrenId()) + "' 情况不属实，现已驳回"))
                    .setConversationId(conversationId)
                    .setReceiverId(userId)
                    .setSenderId(SecurityFrameworkUtil.getLoginUserId()));
        }

        cvReportMapper.insertOrUpdate(cvReportDO);
        return cvReportDO.getId();
    }

    @Override
    public PageResult<AuditReportRespVO> getReportPage(AuditReportPageReqVO reqVO) {
        PageResult<CvReportDO> pageResult = cvReportMapper.selectPage(reqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return PageResult.empty();
        }

        return BeanUtils.toBean(pageResult, AuditReportRespVO.class);
    }

    @Override
    public void deleteAllReport(Long childrenId) {
        // 将所有关联该内容的举报全部设为已处理
        if (ObjUtil.isNotEmpty(childrenId)) {
            LambdaUpdateWrapper<CvReportDO> wrapper = new LambdaUpdateWrapper<CvReportDO>()
                    .set(CvReportDO::getStatus, ReportStatusEnum.HANDLED)
                    .eq(CvReportDO::getChildrenId, childrenId);
            cvReportMapper.update(wrapper);
        }
    }

    @Override
    public void deleteAllReportByUserId(Long userId) {
        if (ObjUtil.isNotEmpty(userId)) {
            LambdaQueryWrapper<CvReportDO> wrapper = new LambdaQueryWrapperX<CvReportDO>()
                    .eq(CvReportDO::getReportUserId, userId)
                    .or()
                    .eq(CvReportDO::getTargetUserId, userId);
            cvReportMapper.delete(wrapper);
        }
    }

}
